package com.jjr.action;

import java.io.IOException;
import java.util.*;
import tool.ClientTool;
import tool.IdcardValidator;

import com.base.bean.*;
import com.base.service.*;
import com.base.dao.*;
import com.jjr.service.JingJiRenService;

/**
 *  配合php 对json的操作
 *  功能：
 *  验证电话号码（格式 重复性）
 *  姓名重复性
 *  身份证号格式验证
 */
public class CheckAction extends BaseAction {
	private String phone;
	private String name;
	private String idCard;


	private JingJiRenService jjs;
	
	

	public CheckAction() {
		jjs = JingJiRenService.getInstance();
		
	}
	
	// check idCard
	public String checkIdCard(){
		boolean checked = false;
		this.idCard  = (this.idCard==null?"":this.idCard.replaceAll("\\s*", ""));
		
		checked = IdcardValidator.getInstance().isValidatedAllIdcard(idCard);
//		System.out.println(checked);
//		System.out.println(this.idCard);
		if(!checked){
			this.result.setState(-1);
			this.result.setTip("身份证号码格式有误");
			return SUCCESS;
		}
		
		int count = jjs.getUdao().findByHql("from JUser u where u.idCard='"+this.idCard+"'").size();
		if(count>0){
			this.result.setState(-2);
			this.result.setTip("该身份证号码已经存在");
			return SUCCESS;
		}
		
		this.result.setState(1);
		return SUCCESS;
		
	}
	
	

	// check name
	public String checkName(){
		this.name = (this.name==null?"":this.name.replaceAll("\\s*", ""));
		if(this.name.equals("")){
			this.result.setState(-1);
			this.result.setErrCode(-1);
			this.result.setTip("姓名不能为空");
			return SUCCESS;
		}
		
		// is hanzi
		String reg = "[\\u4e00-\\u9fa5]{2,}";// 表示+表示一个或多个中文   {2,}两个以上汉字
		boolean isHanzi = this.name.matches(reg);
		if(!isHanzi){
			this.result.setState(-2);
			this.result.setErrCode(-2);
			this.result.setTip("姓名不能有非中文字符");
			return SUCCESS;
		}
		
		
		// name repeat TiXing
		int a = jjs.getCbdao().findByHql("from ClientBase a where a.name='"+this.name+"'").size();
		int b = jjs.getCdao().findByHql("from JClient b where b.name='"+this.name+"'").size();
		
		if(a>0){
			this.result.setState(-3);
			this.result.setErrCode(-3);
			this.result.setTip("已经存在同名客户,请继续完善信息.");
			return SUCCESS;
		}else if(b>0){
			this.result.setState(-4);
			this.result.setErrCode(-4);
			this.result.setTip("已经存在同名客户,请继续完善信息..");
			return SUCCESS;
		}
		this.result.setErrCode(0);
		this.result.setTip("");
		this.result.setState(1);
		return SUCCESS;
	}
	
	public String checkFormatName(){
		this.name = (this.name==null?"":this.name.replaceAll("\\s*", ""));
		if(this.name.equals("")){
			this.result.setState(-1);
			this.result.setErrCode(-1);
			this.result.setTip("姓名不能为空");
			return SUCCESS;
		}
		
		// is hanzi
		String reg = "[\\u4e00-\\u9fa5]{2,}";// 表示+表示一个或多个中文   {2,}两个以上汉字
		boolean isHanzi = this.name.matches(reg);
		if(!isHanzi){
			this.result.setState(-2);
			this.result.setErrCode(-2);
			this.result.setTip("姓名不能有非中文字符");
			return SUCCESS;
		}
		this.result.setErrCode(0);
		this.result.setTip("");
		this.result.setState(1);
		return SUCCESS;
	}
	
	public String checkFormatPhone(){
		this.phone = (this.phone==null?"":this.phone.replaceAll("\\s*", ""));
		if(this.phone.equals("")){
			this.result.setState(-1);
			this.result.setErrCode(-1);
			this.result.setTip("电话号码不能为空");
			return SUCCESS;
		}
		
		boolean isMobile = ClientTool.isMobileNO(phone);
		
		if(!isMobile){
			this.result.setState(-2);
			this.result.setErrCode(-2);
			this.result.setTip("电话格式错误");
			return SUCCESS;
		}
		
		int count = jjs.getUdao().findByHql("from JUser u where u.phone='"+this.phone+"'").size();
		if(count>0){
			this.result.setState(-3);
			this.result.setErrCode(-2);
			this.result.setTip("该号码已经被注册");
			return SUCCESS;
		}
		
		
		
		this.result.setState(1);
		this.result.setErrCode(1);
		return SUCCESS;
		
	}
	
	// check phone
	@Override
	public String execute() {
		this.phone = (this.phone==null?"":this.phone.replaceAll("\\s*", ""));
		if(this.phone.equals("")){
			this.result.setState(-1);
			this.result.setErrCode(-1);
			this.result.setTip("电话号码不能为空");
			return SUCCESS;
		}
		
		boolean isMobile = ClientTool.isMobileNO(phone);
		
		if(!isMobile){
			this.result.setState(-4);
			this.result.setErrCode(-4);
			this.result.setTip("电话格式错误");
			return SUCCESS;
		}
		
		int a = jjs.getCpdao().findByHql("from ClientPhone a where a.state=1 and a.phone='"+this.phone+"'").size();
		int b = jjs.getJpdao().findByHql("from JClientPhone b where b.state=1 and  b.phone='"+this.phone+"'").size();
		
		if(a>0 ){
			this.result.setState(-2);
			this.result.setErrCode(-2);
			this.result.setTip("该客户电话已被人录入.");
			return SUCCESS;
		}else if(b>0){
			this.result.setState(-3);
			this.result.setErrCode(-3);
			this.result.setTip("该客户电话已被人录入..");
			return SUCCESS;
		}
		this.result.setErrCode(0);
		this.result.setTip("恭喜您发现一个新客户");
		this.result.setState(1);
		return SUCCESS;

	}



	public String getPhone() {
		return phone;
	}



	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}

	

	
	


}
